package com.oracle.outsidein.process;

import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Read all lines from an input stream.
 */
@Slf4j
public class LinesStreamPumper extends StreamPumper {

    private static class LinesConsumer implements LineConsumer {

        private final List<String> lines = new ArrayList<>();

        @Override
        public void consume(final String line) {
            log.debug(line.trim());
            lines.add(line.trim());
        }
    }

    /**
     * Creates a new pumper for the specified stream.
     *
     * @param stream The input stream to read from.
     */
    public LinesStreamPumper(final InputStream stream) {
        super(stream, new LinesConsumer());
    }

    /**
     * Gets the lines read from the stream by this pumper.
     *
     * @return The command output lines.
     */
    public List<String> getLines() {
        return ((LinesConsumer) getConsumer()).lines;
    }
}
